package student.utils;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author yangdiankang@51tiangou.com
 * @date 2020/5/9.
 */
public class FileUtil {
    public List<String> read(String path) {
        try {
            FileReader fr = new FileReader(path);
            BufferedReader br=new BufferedReader(fr);
            List<String> lines = new ArrayList<>();
            String line;
            while ((line = br.readLine()) != null) {
                lines.add(line);
            }

            br.close();
            fr.close();
            return lines;
        } catch (Exception e) {
            String message = e.getMessage();
            if (!Objects.equals(null, message) && message.contains("系统找不到指定的文件")) {
                init(path);
                return read(path);
            } else {
                System.out.println(e.getMessage());
            }
        }

        return null;
    }

    public void write(String path, List<String> texts) throws IOException {
        FileOutputStream fos = new FileOutputStream(path);
        OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
        BufferedWriter bw = new BufferedWriter(osw);

        for (String text : texts) {
            bw.write(text + "\t\n");
        }

        //注意关闭的先后顺序，先打开的后关闭，后打开的先关闭
        bw.close();
        osw.close();
        fos.close();


    }

    /**
     * 初始化  文件
     *
     * @param path
     */
    private  void init(String path) {
        if (path.contains("student")) {
            initStudent(path);
        } else if (path.contains("teacher")) {
            initTeacher(path);
        } else {
            initBook(path);
        }
    }

    /**
     *  初始化 书本
     * @param path
     */
    private void initBook(String path) {
        List<String> strings = new ArrayList<>();
        strings.add("ID,书名,页数,对应学生ID");
        for (int i = 1; i <= 5; i++) {
            strings.add(i+",书名"+i+",1000,"+i);
        }

        try {
            this.write(path, strings);
        } catch (IOException e) {
            System.out.println("初始化失败");
            e.printStackTrace();
        }
    }

    /**
     *  初始化  学生
     * @param path
     */
    private  void initStudent(String path) {
        List<String> strings = new ArrayList<>();
        strings.add("ID,姓名,年龄,学号,老师Id");
        for (int i = 1; i <= 5; i++) {
            strings.add(i+",学生"+i+",12,学号"+i+ "," + i);
        }

        try {
            this.write(path, strings);
        } catch (IOException e) {
            System.out.println("初始化失败");
            e.printStackTrace();
        }
    }


    /**
     *  初始化  老师
     * @param path
     */
    private  void initTeacher(String path) {
        List<String> strings = new ArrayList<>();
        strings.add("ID,老师姓名,老师年龄,老师工号");
        for (int i = 1; i <= 5; i++) {
            strings.add(i+",老师"+i+",55,工号"+i);
        }

        try {
            this.write(path, strings);
        } catch (IOException e) {
            System.out.println("初始化失败");
            e.printStackTrace();
        }
    }

    public void deleteFile(String studentFilePath) {
        File file = new File(studentFilePath);
        file.delete();
    }


    /**
     * 查询所有信息,并且输出
     */
    public List<String> queryAll(String path) {
        List<String> strings = this.read(path);
        for (String str : strings) {
            System.out.println(str);
        }

        return strings;
    }
}
